package com.groupbuying.sysconfig.controller;

import com.groupbuying.sysconfig.constants.BizTagConstants;
import com.groupbuying.sysconfig.constants.CityConstants;
import com.groupbuying.sysconfig.domain.KeyinfoDO;
import com.groupbuying.sysconfig.service.KeyinfoService;
import com.groupbuying.sysconfig.utils.PageUtils;
import com.groupbuying.sysconfig.utils.Query;
import com.groupbuying.sysconfig.utils.R;
import com.groupbuying.sysconfig.domain.O2okindDO;
import com.groupbuying.sysconfig.leaf.SegmentService;
import com.groupbuying.sysconfig.leaf.common.Result;
import com.groupbuying.sysconfig.leaf.common.Status;
import com.groupbuying.sysconfig.service.O2okindService;
import com.groupbuying.sysconfig.utils.ReduceUuidLength;
import com.netflix.discovery.converters.Auto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * 公共方法
 * 
 * @author zmlmagic
 * @email 16651@mengtaigroup.com
 * @date 2019-03-11 18:43:14
 */


@RestController
@RequestMapping("/common")
public class CommonController {

    @Autowired
	private SegmentService segmentService;

    @Autowired
    private KeyinfoService keyinfoService;

	/* 统一控制cityCode的@RequestBody是否必须 */
    private final static boolean cityCodeIsRequired = false;

    /* 统一控制cityCode的@RequestBody是否必须 */
    private final static boolean bizCodeIsRequired = false;

    /* cityCode暂时统一规定呼和浩特 */
    private final static String cityCode = CityConstants.HOHHOT;

    /* bizTag暂时统一规定为够样优店 */
    private final static String bizTag = BizTagConstants.GOUYANG_YOUDIAN;

    /* 传入key、cityCode、bizTag生成Id */
    private String generateId(String key, String cityCode, String bizTag){
        try {
            Result result = segmentService.getId(key);
            Long longResult;
            if(result.getStatus().equals(Status.SUCCESS)){
                longResult = result.getId();

                /* ID格式为：地区码 + 业务码 + 生成码 */
                return cityCode + bizTag + longResult.toString();
            } else {
                /* 缩减UUID由32位到22位，防止id字段爆掉 */
                return cityCode + bizTag + ReduceUuidLength.shorter(UUID.randomUUID().toString());
            }
        } catch (Exception e){
            return cityCode + bizTag + ReduceUuidLength.shorter(UUID.randomUUID().toString());
        }
    }

	/* 获取商户ID */
	@GetMapping("/store/getAcountId")
	public String getAcountId(@RequestBody(required = cityCodeIsRequired) String cityCode
            , @RequestBody(required = bizCodeIsRequired) String bizTag){

	    /* 未传入cityCode则定义为默认呼和浩特 */
	    if(StringUtils.isBlank(cityCode)){
	        cityCode = CommonController.cityCode;
        }

	    /* 未传入bizCode则定义为默认够样优店 */
        if(StringUtils.isBlank(bizTag)){
            bizTag = CommonController.bizTag;
        }

        /* 返回生成ID */
        return generateId("store_acount_id", cityCode, bizTag);

	}

	/* 获取服务商ID */
	@GetMapping("/store/getProviderId")
	public String getProviderId(@RequestBody(required = cityCodeIsRequired) String cityCode
            , @RequestBody(required = bizCodeIsRequired) String bizTag){

        /* 未传入cityCode则定义为默认呼和浩特 */
        if(StringUtils.isBlank(cityCode)){
            cityCode = CommonController.cityCode;
        }

        /* 未传入bizCode则定义为默认够样优店 */
        if(StringUtils.isBlank(bizTag)){
            bizTag = CommonController.bizTag;
        }

        /* 返回生成ID */
        return generateId("store_provider_id", cityCode, bizTag);

	}

	/* 获取门店ID */
	@GetMapping("/store/getStoreNoByLeaf")
	public String getStoreNoByLeaf(@RequestBody(required = cityCodeIsRequired) String cityCode
            , @RequestBody(required = bizCodeIsRequired) String bizTag){

        /* 未传入cityCode则定义为默认呼和浩特 */
        if(StringUtils.isBlank(cityCode)){
            cityCode = CommonController.cityCode;
        }

        /* 未传入bizCode则定义为默认够样优店 */
        if(StringUtils.isBlank(bizTag)){
            bizTag = CommonController.bizTag;
        }

        /* 返回生成ID */
        return generateId("store_shop_id", cityCode, bizTag);
	}

	/* TODO */
	@GetMapping("/store/getCounterNoByLeaf")
	public String getCounterNoByLeaf(@RequestBody(required = cityCodeIsRequired) String cityCode
            , @RequestBody(required = bizCodeIsRequired) String bizTag){
        /* 未传入cityCode则定义为默认呼和浩特 */
        if(StringUtils.isBlank(cityCode)){
            cityCode = CommonController.cityCode;
        }

        /* 未传入bizCode则定义为默认够样优店 */
        if(StringUtils.isBlank(bizTag)){
            bizTag = CommonController.bizTag;
        }

        /* 返回生成ID */
        return generateId("store_provider_id", cityCode, bizTag);
	}

	/* TODO */
	@GetMapping("/store/getPromotiondefineNo")
	public String getPromotiondefineNo(@RequestBody(required = cityCodeIsRequired) String cityCode
            , @RequestBody(required = bizCodeIsRequired) String bizTag){
        /* 未传入cityCode则定义为默认呼和浩特 */
        if(StringUtils.isBlank(cityCode)){
            cityCode = CommonController.cityCode;
        }

        /* 未传入bizCode则定义为默认够样优店 */
        if(StringUtils.isBlank(bizTag)){
            bizTag = CommonController.bizTag;
        }

        /* 返回生成ID */
        return generateId("store_provider_id", cityCode, bizTag);
	}

	/* 获取用户ID */
	@GetMapping("/customer/getCustomerId")
	public String getCustomerId(@RequestBody(required = cityCodeIsRequired) String cityCode
            , @RequestBody(required = bizCodeIsRequired) String bizTag){

        /* 未传入cityCode则定义为默认呼和浩特 */
        if(StringUtils.isBlank(cityCode)){
            cityCode = CommonController.cityCode;
        }

        /* 未传入bizCode则定义为默认够样优店 */
        if(StringUtils.isBlank(bizTag)){
            bizTag = CommonController.bizTag;
        }

        /* 返回生成ID */
        return generateId("customer_id", cityCode, bizTag);
	}

	/* 获取运营活动ID */
	@GetMapping("/currency/createActivityId")
	public String createActivityId(@RequestBody(required = cityCodeIsRequired) String cityCode
            , @RequestBody(required = bizCodeIsRequired) String bizTag){

        /* 未传入cityCode则定义为默认呼和浩特 */
        if(StringUtils.isBlank(cityCode)){
            cityCode = CommonController.cityCode;
        }

        /* 未传入bizCode则定义为默认够样优店 */
        if(StringUtils.isBlank(bizTag)){
            bizTag = CommonController.bizTag;
        }

        /* 返回生成ID */
        return generateId("currency_activity_id", cityCode, bizTag);
	}

	/* 获取日志ID */
    @GetMapping("/currency/createLogId")
    public String createLogId(@RequestBody(required = cityCodeIsRequired) String cityCode
            , @RequestBody(required = bizCodeIsRequired) String bizTag){

        /* 未传入cityCode则定义为默认呼和浩特 */
        if(StringUtils.isBlank(cityCode)){
            cityCode = CommonController.cityCode;
        }

        /* 未传入bizCode则定义为默认够样优店 */
        if(StringUtils.isBlank(bizTag)){
            bizTag = CommonController.bizTag;
        }

        /* 返回生成ID */
        return generateId("currency_log_id", cityCode, bizTag);
    }

    /* 获取订单ID */
	@GetMapping("/order/getOrderId")
	public String getOrderId(@RequestParam(required = cityCodeIsRequired) String cityCode
            , @RequestParam(required = bizCodeIsRequired) String bizTag){

        /* 未传入cityCode则定义为默认呼和浩特 */
        if(StringUtils.isBlank(cityCode)){
            cityCode = CommonController.cityCode;
        }

        /* 未传入bizCode则定义为默认够样优店 */
        if(StringUtils.isBlank(bizTag)){
            bizTag = CommonController.bizTag;
        }

        /* 返回生成ID */
        return generateId("order_id", cityCode, bizTag);
	}
    /* 获取商品子订单ID */
    @GetMapping("/commodity/getSuborderId")
    public String getSuborderId(@RequestParam(required = cityCodeIsRequired) String cityCode
            , @RequestParam(required = bizCodeIsRequired) String bizTag){

        /* 未传入cityCode则定义为默认呼和浩特 */
        if(StringUtils.isBlank(cityCode)){
            cityCode = CommonController.cityCode;
        }

        /* 未传入bizCode则定义为默认够样优店 */
        if(StringUtils.isBlank(bizTag)){
            bizTag = CommonController.bizTag;
        }

        /* 返回生成ID */
        return generateId("commodity_suborder_id", cityCode, bizTag);
    }

	/* 获取支付ID */
	@GetMapping("/pay/getPayId")
	public String getPayId(@RequestBody(required = cityCodeIsRequired) String cityCode
            , @RequestBody(required = bizCodeIsRequired) String bizTag){

        /* 未传入cityCode则定义为默认呼和浩特 */
        if(StringUtils.isBlank(cityCode)){
            cityCode = CommonController.cityCode;
        }

        /* 未传入bizCode则定义为默认够样优店 */
        if(StringUtils.isBlank(bizTag)){
            bizTag = CommonController.bizTag;
        }

        /* 返回生成ID */
        return generateId("pay_id", cityCode, bizTag);
	}

	/* 获取结算ID */
	@GetMapping("/pay/getSettleId")
	public String getSettleId(@RequestBody(required = cityCodeIsRequired) String cityCode
            , @RequestBody(required = bizCodeIsRequired) String bizTag){

        /* 未传入cityCode则定义为默认呼和浩特 */
        if(StringUtils.isBlank(cityCode)){
            cityCode = CommonController.cityCode;
        }

        /* 未传入bizCode则定义为默认够样优店 */
        if(StringUtils.isBlank(bizTag)){
            bizTag = CommonController.bizTag;
        }

        /* 返回生成ID */
        return generateId("settle_id", cityCode, bizTag);
	}

    /* 获取流水ID */
    @GetMapping("/flow/getFlowId")
    public String getFlowId(@RequestBody(required = cityCodeIsRequired) String cityCode
            , @RequestBody(required = bizCodeIsRequired) String bizTag){

        /* 未传入cityCode则定义为默认呼和浩特 */
        if(StringUtils.isBlank(cityCode)){
            cityCode = CommonController.cityCode;
        }

        /* 未传入bizCode则定义为默认够样优店 */
        if(StringUtils.isBlank(bizTag)){
            bizTag = CommonController.bizTag;
        }

        /* 返回生成ID */
        return generateId("flow_id", cityCode, bizTag);
    }

    /* 获取退款单ID */
    @GetMapping("/refund/getRefundId")
    public String getRefundId(@RequestBody(required = cityCodeIsRequired) String cityCode
            , @RequestBody(required = bizCodeIsRequired) String bizTag){

        /* 未传入cityCode则定义为默认呼和浩特 */
        if(StringUtils.isBlank(cityCode)){
            cityCode = CommonController.cityCode;
        }

        /* 未传入bizCode则定义为默认够样优店 */
        if(StringUtils.isBlank(bizTag)){
            bizTag = CommonController.bizTag;
        }

        /* 返回生成ID */
        return generateId("settle_id", cityCode, bizTag);
    }

    /* 获取提现ID */
    @GetMapping("/settle/getWithdrawalId")
    public String getWithdrawalId(@RequestBody(required = cityCodeIsRequired) String cityCode
            , @RequestBody(required = bizCodeIsRequired) String bizTag){

        /* 未传入cityCode则定义为默认呼和浩特 */
        if(StringUtils.isBlank(cityCode)){
            cityCode = CommonController.cityCode;
        }

        /* 未传入bizCode则定义为默认够样优店 */
        if(StringUtils.isBlank(bizTag)){
            bizTag = CommonController.bizTag;
        }

        /* 返回生成ID */
        return generateId("withdrawal_id", cityCode, bizTag);
    }

    /* 获取分账规则ID */
    @GetMapping("/settle/getSettleRuleId")
    public String getSettleRuleId(@RequestBody(required = cityCodeIsRequired) String cityCode
            , @RequestBody(required = bizCodeIsRequired) String bizTag){

        /* 未传入cityCode则定义为默认呼和浩特 */
        if(StringUtils.isBlank(cityCode)){
            cityCode = CommonController.cityCode;
        }

        /* 未传入bizCode则定义为默认够样优店 */
        if(StringUtils.isBlank(bizTag)){
            bizTag = CommonController.bizTag;
        }

        /* 返回生成ID */
        return generateId("settle_rule_id", cityCode, bizTag);
    }

    /* 获取银盛账号 */
    @GetMapping("/ys/getPayeeUsercode")
    public String getPayeeUsercode(){

        Map<String, Object> map = new HashMap<>();
        map.put("infoKind", "ys_platform_account");
        map.put("infoKey", "GYYOUDIAN");

        KeyinfoDO keyinfoDO = keyinfoService.list(map).get(0);

        return keyinfoDO.getInfoValue();
    }

    /* 获取银盛平台业务识别号 */
    @GetMapping("/ys/getBusinessCode")
    public String getBusinessCode(){

        Map<String, Object> map = new HashMap<>();
        map.put("infoKind", "ys_platform_account");
        map.put("infoKey", "BUSINESS_CODE");

        KeyinfoDO keyinfoDO = keyinfoService.list(map).get(0);

        return keyinfoDO.getInfoValue();
    }

    /* 获取在银盛开设的平台账号 */
    @GetMapping("/ys/getPlanformCode")
    public String getPlanformCode(){
        Map<String, Object> map = new HashMap<>();
        map.put("infoKind", "ys_platform_account");
        map.put("infoKey", "PLANFORMCODE");
        KeyinfoDO keyinfoDO = keyinfoService.list(map).get(0);
        return keyinfoDO.getInfoValue();
    }
    /* 获取在银盛开设的营销账号 */
    @GetMapping("/ys/getYSgymarketing")
    public String getYSgymarketing(){
        Map<String, Object> map = new HashMap<>();
        map.put("infoKind", "ys_platform_account");
        map.put("infoKey", "GYMARKETING");
        KeyinfoDO keyinfoDO = keyinfoService.list(map).get(0);
        return keyinfoDO.getInfoValue();
    }
    /* 获取代理商ID */
    @GetMapping("/admin/getAgentId")
    public String getAgentId(@RequestBody(required = cityCodeIsRequired) String cityCode
            , @RequestBody(required = bizCodeIsRequired) String bizTag){

        /* 未传入cityCode则定义为默认呼和浩特 */
        if(StringUtils.isBlank(cityCode)){
            cityCode = CommonController.cityCode;
        }

        /* 未传入bizCode则定义为默认够样优店 */
        if(StringUtils.isBlank(bizTag)){
            bizTag = CommonController.bizTag;
        }

        /* 返回生成ID */
        return generateId("admin_agent_id", cityCode, bizTag);
    }

    /* 获取在马拉松游戏门店名称----临时3天可用 */
    @GetMapping("/marathon/getStoreName")
    public String getStoreName(){
        Map<String, Object> map = new HashMap<>();
        map.put("infoKind", "ordos_marathon_storename");
        map.put("infoKey", "STORENAME");
        KeyinfoDO keyinfoDO = keyinfoService.list(map).get(0);
        return keyinfoDO.getInfoValue();
    }
}
